﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Common;
//test
namespace DataAccessLayer
{
    public class Master_Item_Types_DB
    {
        Database objDatabase = null;
        DbCommand ObjCommand = null;
        MethodResult ObjMethodResult = null;


        public const string ADDMASTERITEMTYPE = "AddMasterType";
        public const string UPDATEMASTERITEMTYPE = "UpdateMasterType";
        public const string DELETEMASTERITEMTYPE = "DeleteMasterType";

        public Master_Item_Types_DB()
        {
            // Used to connect to more than one database
            DatabaseProviderFactory Objfactory = new DatabaseProviderFactory();
            objDatabase = Objfactory.CreateDefault();
            ObjMethodResult = new MethodResult();
        }

        public List<Master_Item_Types> GetMasterItemTypes()
        {
            string query = "Select Master_Item,Item_Desc,Status,Modified_On,Modified_By from Master_Item_Types";
            var listusers = objDatabase.ExecuteSqlStringAccessor<Master_Item_Types>(query);
            return listusers.ToList<Master_Item_Types>();

        }

              public MethodResult AddMasterItemType(string item_Type, string desc, string user_id, out string result_Message)
        {
            ObjCommand = objDatabase.GetStoredProcCommand(ADDMASTERITEMTYPE);
            objDatabase.AddInParameter(ObjCommand, "Master_Item", DbType.String, item_Type);
            objDatabase.AddInParameter(ObjCommand, "Desc", DbType.String, desc);
            objDatabase.AddInParameter(ObjCommand, "User_Id", DbType.String, user_id);
            objDatabase.AddOutParameter(ObjCommand, "Return_Message", DbType.String, 100);
            short result = (short)objDatabase.ExecuteNonQuery(ObjCommand);

            result_Message = objDatabase.GetParameterValue(ObjCommand, "Return_Message").ToString();
            if (result < 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Warning, result_Message, null, result);
            else if (result > 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Success, result_Message, null, result);
            else
                return ObjMethodResult.CurrentMessageResult(MessageType.Information, result_Message, null, result);
            
        }

        public MethodResult UpdateMasterItemType(string item_Type, string desc, string user_id, out string result_Message)
        {
            ObjCommand = objDatabase.GetStoredProcCommand(UPDATEMASTERITEMTYPE);
            objDatabase.AddInParameter(ObjCommand, "Master_Item", DbType.String, item_Type);
            objDatabase.AddInParameter(ObjCommand, "Desc", DbType.String, desc);
            objDatabase.AddInParameter(ObjCommand, "User_Id", DbType.String, user_id);
            objDatabase.AddOutParameter(ObjCommand, "Return_Message", DbType.String, 100);
            short result = (short)objDatabase.ExecuteNonQuery(ObjCommand);

            result_Message = objDatabase.GetParameterValue(ObjCommand, "Return_Message").ToString();
            if (result < 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Warning, result_Message, null, result);
            else if (result > 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Success, result_Message, null, result);
            else
                return ObjMethodResult.CurrentMessageResult(MessageType.Information, result_Message, null, result);
        }

        public MethodResult DeleteMasterItemType(string item_Type, string user_id, out string result_Message)
        {
            ObjCommand = objDatabase.GetStoredProcCommand(DELETEMASTERITEMTYPE);
            objDatabase.AddInParameter(ObjCommand, "Master_Item", DbType.String, item_Type);
            objDatabase.AddInParameter(ObjCommand, "User_Id", DbType.String, user_id);
            objDatabase.AddOutParameter(ObjCommand, "Return_Message", DbType.String, 100);
            short result =(short) objDatabase.ExecuteNonQuery(ObjCommand);
            result_Message = objDatabase.GetParameterValue(ObjCommand, "Return_Message").ToString();
            if(result<0)
            return ObjMethodResult.CurrentMessageResult(MessageType.Warning, result_Message, null, result);
            else if(result>0)
            return ObjMethodResult.CurrentMessageResult(MessageType.Success, result_Message, null, result);
            else
            return ObjMethodResult.CurrentMessageResult(MessageType.Information, result_Message, null, result);

        }

    }
}
